package com.zzyl.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.UserDto;
import com.zzyl.dto.UserDto;
import com.zzyl.service.UserService;
import com.zzyl.vo.UserVo;
import com.zzyl.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Api(tags = "用户管理")
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /***
     *  多条件查询用户分页列表
     * @param userDto 用户DTO查询条件
     * @param pageNum 页码
     * @param pageSize 每页条数
     * @return: Page<UserVo>
     */
    @PostMapping("page/{pageNum}/{pageSize}")
    @ApiOperation(value = "用户分页", notes = "用户分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userDto", value = "用户DTO对象", required = true, dataType = "UserDto"),
            @ApiImplicitParam(paramType = "path", name = "pageNum", value = "页码", example = "1", dataType = "Integer"),
            @ApiImplicitParam(paramType = "path", name = "pageSize", value = "每页条数", example = "10", dataType = "Integer")
    })
    @ApiOperationSupport(includeParameters = {"userDto.username", "userDto.email", "userDto.dataState", "userDto.deptNo"})
    public ResponseResult<PageResponse<UserVo>> findUserVoPage(
            @RequestBody UserDto userDto,
            @PathVariable("pageNum") int pageNum,
            @PathVariable("pageSize") int pageSize) {
        PageResponse<UserVo> userVoPage = userService.findUserPage(userDto, pageNum, pageSize);
        return ResponseResult.success(userVoPage);
    }

    /**
     * 用户添加
     *
     * @param userDto 用户Vo对象
     * @return UserDto
     */
    @PutMapping
    @ApiOperation(value = "用户添加", notes = "用户添加")
    @ApiImplicitParam(name = "userDto", value = "用户DTO对象", required = true, dataType = "UserDto")
    @ApiOperationSupport(includeParameters = {"userDto.email", "userDto.dataState", "userDto.deptNo", "userDto.deptPostUserVoSet", "userDto.mobile", "userDto.postNo", "userDto.realName", "userDto.roleVoIds"})
    public ResponseResult<UserVo> createUser(@RequestBody UserDto userDto) {
        UserVo userVoResult = userService.createUser(userDto);
        return ResponseResult.success(userVoResult);
    }

    /**
     * 用户修改
     */
    @PatchMapping
    @ApiOperation(value = "用户修改", notes = "用户修改")
    public ResponseResult<Integer> updateUser(@RequestBody UserDto userDto) {
        Integer i = userService.updateUser(userDto);
        return ResponseResult.success(i);
    }

    /**
     * 当前用户
     */
    @GetMapping("/current-user")
    @ApiOperation(value = "当前用户", notes = "当前用户")
    public ResponseResult<UserVo> getCurrentUser() {
        UserVo userVo = userService.getCurrentUser();
        return ResponseResult.success(userVo);
    }

    /**
     * 启用或禁用用户
     */
    @PutMapping("/is-enable/{id}/{status}")
    @ApiOperation(value = "启用或禁用用户", notes = "启用或禁用用户")
    public ResponseResult<Integer> updateUserStatus(@PathVariable("id") Long id, @PathVariable("status") String status) {
        Integer result = userService.updateUserStatus(id, status);
        return ResponseResult.success(result);
    }

    /**
     * 删除用户
     */
    @DeleteMapping("/remove/{userId}")
    @ApiOperation(value = "删除用户", notes = "删除用户")
    public ResponseResult<Integer> deleteUser(@PathVariable("userId") Long userId) {
        Integer result = userService.deleteUser(userId);
        return ResponseResult.success(result);
    }

    /**
     * 密码重置
     */
    @PostMapping("/reset-passwords/{userId}")
    @ApiOperation(value = "密码重置", notes = "密码重置")
    public ResponseResult<Integer> resetPassword(@PathVariable("userId") Long userId) {
        Integer result = userService.resetPassword(userId);
        return ResponseResult.success(result);
    }

    @PostMapping("/list")
    @ApiOperation(value = "获取用户列表", notes = "用于下拉选择、快速搜索等场景")
    public ResponseResult<List<UserVo>> getUserList(@RequestBody UserDto userDto) {
        List<UserVo> userList = userService.getUserList(userDto);
        return ResponseResult.success(userList);
    }

}
